package controllers;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 * The Class MergePhoto.
 * @author Dmitry
 */
public class MergePhoto {
	
	/** The new image. */
	BufferedImage newImage = null;
	
	/** The source image. */
	BufferedImage sourceImage = null;
	
	/**
	 * Instantiates a new merge photo.
	 */
	public MergePhoto(){
		
	}
	
	/**
	 * Merge layer.
	 *
	 * @param image the image
	 * @param layer the layer
	 * @return the buffered image
	 */
	public BufferedImage mergeLayer(BufferedImage image,BufferedImage layer){
		// This code is merging an image and a layer into one photo
		if(newImage == null){
			newImage = copyImage(image);
			sourceImage = copyImage(image);
		}
		for( int i = 0; i < image.getWidth(); i++ )
		    for( int j = 0; j < image.getHeight() ; j++ )
		       if((layer.getRGB(i, j) == -16777216) || (layer.getRGB(i, j) == -5360))
		    	   newImage.setRGB(i, j, layer.getRGB(i, j));
		return newImage;
	}
	
	/**
	 * Copy image.
	 *
	 * @param img the img
	 * @return the buffered image
	 */
	private BufferedImage copyImage(BufferedImage img){
		BufferedImage temp = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
	    Graphics g = temp.getGraphics();
	    g.drawImage(img, 0, 0, null);
	    g.dispose();
	    return temp;
	}
	
	
	/**
	 * Reset photo.
	 */
	public void resetPhoto(){
		this.newImage = copyImage(sourceImage);
	}
	
	
	
}


